package com.hspedu.tankgame4;

import java.util.Vector;

/**
 * @author DL5O
 * @version 1.0
 * 敌人的坦克
 */
public class EnemyTank extends Tank implements Runnable {
    /*//在敌人坦克类，使用vector 保存多个Shot
    Shot shot;*/
    //通过set方法设置enemyTanks
    static Vector<EnemyTank> enemyTanks = new Vector<>();

    //设置到 EnemyTank 的成员 enemyTanks
    public static void setEnemyTanks(Vector<EnemyTank> newEnemyTanks) {
        enemyTanks = newEnemyTanks;
    }

    //编写方法，判断当前的这个坦克是否和 其他的enemyTank发生重叠，或者碰撞
    public boolean isOverlapEnemyTank() {
        switch (getDirect()) {
            case 0://上
                //让当前tank和所有坦克进行对比
                for (int i = 0; i < enemyTanks.size(); i++) {
                    if (enemyTanks.get(i).equals(this)) {
                        continue;
                    }
                    EnemyTank other = enemyTanks.get(i);
                    switch (other.getDirect()) {
                        case 0://上
                        case 2://下
                            //1.敌人tank 如果是上下: x的范围为 [x,x+40]
                            //                     y的范围为 [y,y+60]
                            //2.当前tank的左上角坐标 [this.getx(),this.gety()]
                            //           右上角坐标 [this.getx()+40,this.gety()]
                            if (this.getX() >= other.getX()
                                    && this.getX() <= other.getX() + 40
                                    && this.getY() >= other.getY()
                                    && this.getY() <= other.getY() + 60
                            ) {
                                return true;
                            }
                            if (this.getX() + 40 >= other.getX()
                                    && this.getX() + 40 <= other.getX() + 40
                                    && this.getY() >= other.getY()
                                    && this.getY() <= other.getY() + 60
                            ) {
                                return true;
                            }
                            break;
                        case 1://右
                        case 3://左
                            if (this.getX() >= other.getX()
                                    && this.getX() <= other.getX() + 60
                                    && this.getY() >= other.getY()
                                    && this.getY() <= other.getY() + 40
                            ) {
                                return true;
                            }

                            if (this.getX() + 40 >= other.getX()
                                    && this.getX() + 40 <= other.getX() + 60
                                    && this.getY() >= other.getY() + 40
                                    && this.getY() <= other.getY() + 40
                            ) {
                                return true;
                            }
                            break;
                    }
                }
                break;
            case 1://右
                for (int i = 0; i < enemyTanks.size(); i++) {
                    if (enemyTanks.get(i).equals(this)) {
                        continue;
                    }
                    EnemyTank other = enemyTanks.get(i);
                    switch (other.getDirect()) {
                        case 0://上
                        case 2://下
                            /*
                            1.敌人tank 如果是上下:  x的范围为 [other.x,other.x+40]
                                                 y的范围为 [other.y,other.y+60]
                                      如果是左右:  x的范围为 [other.x,other.x+60]
                                                 y的范围为[other.y,other.y+40]
                            2.当前tank的右上角坐标 [this.getx() + 60,this.gety()]
                                       右下角坐标 [this.getx() + 60,this.gety() + 40]

                             */
                            if (this.getX() + 60 >= other.getX()
                                    && this.getX() + 60 <= other.getX() + 40
                                    && this.getY() >= other.getY()
                                    && this.getY() <= other.getY() + 60
                            ) {
                                return true;
                            }

                            if (this.getX() + 60 >= other.getX()
                                    && this.getX() + 60 <= other.getX() + 40
                                    && this.getY() + 60 >= other.getY()
                                    && this.getY() + 60 <= other.getY() + 60
                            ) {
                                return true;
                            }

                            break;
                        case 1://右
                        case 3://左
                            if (this.getX() + 60 >= other.getX()
                                    && this.getX() + 60 <= other.getX() + 60
                                    && this.getY() >= other.getY()
                                    && this.getY() <= other.getY() + 40
                            ) {
                                return true;
                            }

                            if (this.getX() + 60 >= other.getX()
                                    && this.getX() + 60 <= other.getX() + 60
                                    && this.getY() + 60 >= other.getY()
                                    && this.getY() + 60 <= other.getY() + 40
                            ) {
                                return true;
                            }
                            break;
                    }
                }
                break;
            case 2://下
                for (int i = 0; i < enemyTanks.size(); i++) {
                    if (enemyTanks.get(i).equals(this)) {
                        continue;
                    }
                    EnemyTank other = enemyTanks.get(i);
                    switch (other.getDirect()) {
                        case 0://上
                        case 2://下
                            /*
                            1.敌人tank 如果是上下:  x的范围为 [other.x,other.x+40]
                                                 y的范围为 [other.y,other.y+60]
                                      如果是左右:  x的范围为 [other.x,other.x+60]
                                                 y的范围为[other.y,other.y+40]
                            2.当前tank的左下角坐标 [this.getx(),this.gety()+60]
                                       右下角坐标 [this.getx() + 40,this.gety()+60]

                             */
                            if (this.getX() >= other.getX()
                                    && this.getX() <= other.getX() + 40
                                    && this.getY() + 60 >= other.getY()
                                    && this.getY() + 60 <= other.getY() + 60
                            ) {
                                return true;
                            }

                            if (this.getX() + 40 >= other.getX()
                                    && this.getX() + 40 <= other.getX() + 40
                                    && this.getY() + 60 >= other.getY()
                                    && this.getY() + 60 <= other.getY() + 60
                            ) {
                                return true;
                            }

                            break;
                        case 1://右
                        case 3://左
                            if (this.getX() + 60 >= other.getX()
                                    && this.getX() + 60 <= other.getX() + 60
                                    && this.getY() >= other.getY()
                                    && this.getY() <= other.getY() + 40
                            ) {
                                return true;
                            }

                            if (this.getX() + 60 >= other.getX()
                                    && this.getX() + 60 <= other.getX() + 60
                                    && this.getY() + 60 >= other.getY()
                                    && this.getY() + 60 <= other.getY() + 40
                            ) {
                                return true;
                            }
                            break;
                        default:
                            break;
                    }
                }
                break;
            case 3://左
                for (int i = 0; i < enemyTanks.size(); i++) {
                    if (enemyTanks.get(i).equals(this)) {
                        continue;
                    }
                    EnemyTank other = enemyTanks.get(i);
                    switch (other.getDirect()) {
                        case 0://上
                        case 2://下
                            /*
                            1.敌人tank 如果是上下:  x的范围为 [other.x,other.x+40]
                                                 y的范围为 [other.y,other.y+60]
                                      如果是左右:  x的范围为 [other.x,other.x+60]
                                                 y的范围为[other.y,other.y+40]
                            2.当前tank的左上角坐标 [this.getx(),this.gety()]
                                       左下角坐标 [this.getx(),this.gety()+60]

                             */
                            if (this.getX() >= other.getX()
                                    && this.getX() <= other.getX() + 40
                                    && this.getY() >= other.getY()
                                    && this.getY() <= other.getY() + 60
                            ) {
                                return true;
                            }

                            if (this.getX() >= other.getX()
                                    && this.getX() <= other.getX() + 40
                                    && this.getY() + 40 >= other.getY()
                                    && this.getY() + 40 <= other.getY() + 60
                            ) {
                                return true;
                            }

                            break;
                        case 1://右
                        case 3://左
                            if (this.getX() >= other.getX()
                                    && this.getX() <= other.getX() + 60
                                    && this.getY() >= other.getY()
                                    && this.getY() <= other.getY() + 40
                            ) {
                                return true;
                            }

                            if (this.getX() >= other.getX()
                                    && this.getX() <= other.getX() + 60
                                    && this.getY() + 40 >= other.getY()
                                    && this.getY() + 40 <= other.getY() + 40
                            ) {
                                return true;
                            }
                            break;
                        default:
                            break;
                    }
                }
                break;
        }
        return false;
    }


    public EnemyTank(int x, int y, int type, int speed) {
        super(x, y, type, speed);
    }


    /**
     * 坦克根据不同的方向进行移动，
     *
     * @throws InterruptedException
     */
    public void enemyMove() throws InterruptedException {
        switch (getDirect()) {
            case 0://向上移动
                //让坦克保持一个方向走30秒
                for (int i = 0; i < 30; i++) {
                    if (getY() > 0 && !isOverlapEnemyTank()) {
                        Thread.sleep(50);
                        moveUp();
                    } else {
                        break;
                    }
                }
                break;
            case 1://向右移动
                for (int i = 0; i < 30; i++) {
                    if (getX() + 60 < 1000 && !isOverlapEnemyTank()) {
                        Thread.sleep(50);
                        moveRight();
                    } else {
                        break;
                    }

                }
                break;
            case 2://向下移动
                for (int i = 0; i < 30; i++) {
                    if (getY() + 60 < 750 && !isOverlapEnemyTank()) {
                        moveDown();
                        Thread.sleep(50);
                    } else {
                        break;
                    }

                }
                break;
            case 3://向左移动
                for (int i = 0; i < 30; i++) {
                    if (getX() > 0 && !isOverlapEnemyTank()) {
                        moveLeft();
                        Thread.sleep(50);
                    } else {
                        break;
                    }
                }
                break;
            default:
                //还没有做处理
                break;
        }
    }

    @Override
    public void shotTank() {
        super.shotTank();
    }

    @Override
    public void run() {
        while (true) {
            //这里我们判断如果shots size() = 0,创建一颗子弹，就放入到shots 集合中去
            if (isLive && shots.size() < 10) {
                shotTank();
            }
            try {

                enemyMove();

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //随机生成0~3的随机数
            int enemyDirect = (int) (Math.random() * 4);
            setDirect(enemyDirect);

            //当坦克被击中，生命周期为false
            if (!isLive) {
                break;
            }
        }
    }
}
